package com.iflytek.ci.cpms.dao.impl;

import com.iflytek.ci.cpms.dao.MilestoneBatchDao;
import com.iflytek.ci.cpms.dao.MilestoneDao;
import com.iflytek.ci.cpms.domain.Milestone;
import com.iflytek.ci.cpms.dto.MilestoneCateCountDto;
import com.iflytek.ci.qams.dao.IssueDao;
import com.iflytek.ci.qams.domain.Issue;
import com.iflytek.ci.qams.dto.IssueMilestone;
import com.iflytek.framework.pagination.PaginationResult;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

@Repository("MilestoneBatchDao")
public class MilestoneBatchDaoImpl implements MilestoneBatchDao {
	@Qualifier("batchSqlSession")
	@Autowired
	private SqlSessionTemplate batchSqlSession;

	// @Override
	// public int batch_counts_delete(long[] issue_ids) {
	// SqlSession sqlSession = batchSqlSession.getSqlSessionFactory()
	// .openSession(ExecutorType.BATCH);
	// MilestoneDao milestoneDao = sqlSession.getMapper(MilestoneDao.class);
	// for (long id : issue_ids) {
	// Issue issue = issueDao.issue_with_id(id);
	// if (issue.getMilestone() != 0) {
	// milestoneDao.reduce_counts(issue.getMilestone());
	// }
	// }
	// sqlSession.flushStatements();
	// sqlSession.commit();
	// sqlSession.close();
	// }

	@Override
	public void update_cate_count(long milestone_id, long[] issue_ids) {
		SqlSession sqlSession = batchSqlSession.getSqlSessionFactory()
				.openSession(ExecutorType.BATCH);
		final IssueDao issueDao = sqlSession.getMapper(IssueDao.class);
		final MilestoneDao milestoneDao = sqlSession
				.getMapper(MilestoneDao.class);
		final PaginationResult<IssueMilestone> issueMilestonePaginationResult = issueDao
				.list_page_issue_milestones(issue_ids);
		MilestoneCateCountDto cateCountDto;
		for (IssueMilestone issueMilestone : issueMilestonePaginationResult) {
			Issue issue = issueMilestone.getIssue();
			// 0 表示问题不存在于任何一个里程碑
			if (issue.getMilestone() > 0) {
				milestoneDao.reduce_counts(issue.getMilestone());
				Milestone milestone = milestoneDao.find_milestone_with_id(issue
						.getMilestone());
				if (milestone != null) {
					cateCountDto = milestone.getCate_count();
					// 1 表示打开总数减去1
					cateCountDto.setOpen(cateCountDto.getOpen() - 1);
					milestone.setCate_count(cateCountDto);
					milestoneDao.update_milestone(milestone);
				}
			}
			// 如果不存在里程碑中

		}
		final Milestone milestone_new = milestoneDao
				.find_milestone_with_id(milestone_id);
		cateCountDto = milestone_new.getCate_count();
		if (cateCountDto == null) {
			// 如果不存在里程碑分类统计信息
			cateCountDto = new MilestoneCateCountDto();
		}
		cateCountDto.setOpen(cateCountDto.getOpen()
				+ issueMilestonePaginationResult.size());
		milestone_new.setCate_count(cateCountDto);
		milestoneDao.update_milestone(milestone_new);
		sqlSession.flushStatements();
		sqlSession.commit();
		sqlSession.close();
	}

	@Override
	public void close_cate_count(long[] issue_ids) {
		SqlSession sqlSession = batchSqlSession.getSqlSessionFactory()
				.openSession(ExecutorType.BATCH);
		final IssueDao issueDao = sqlSession.getMapper(IssueDao.class);
		final MilestoneDao milestoneDao = sqlSession
				.getMapper(MilestoneDao.class);
		final PaginationResult<IssueMilestone> issueMilestonePaginationResult = issueDao
				.list_page_issue_milestones(issue_ids);
		MilestoneCateCountDto cateCountDto;
		for (IssueMilestone issueMilestone : issueMilestonePaginationResult) {
			Issue issue = issueMilestone.getIssue();
			if (issue.getMilestone() > 0) {
				Milestone milestone = milestoneDao.find_milestone_with_id(issue
						.getMilestone());
				if (milestone != null) {
					cateCountDto = milestone.getCate_count();
				} else {
					cateCountDto = new MilestoneCateCountDto();
				}
				cateCountDto.setOpen(cateCountDto.getOpen() - 1);
				cateCountDto.setClosed(cateCountDto.getClosed() + 1);
				milestone.setCate_count(cateCountDto);
				milestoneDao.update_milestone(milestone);
			}
		}
		sqlSession.flushStatements();
		sqlSession.commit();
		sqlSession.close();
	}

	@Override
	public void reopen_cate_count(long[] issue_ids) {
		SqlSession sqlSession = batchSqlSession.getSqlSessionFactory()
				.openSession(ExecutorType.BATCH);
		final IssueDao issueDao = sqlSession.getMapper(IssueDao.class);
		final MilestoneDao milestoneDao = sqlSession
				.getMapper(MilestoneDao.class);
		final PaginationResult<IssueMilestone> issueMilestonePaginationResult = issueDao
				.list_page_issue_milestones(issue_ids);
		MilestoneCateCountDto cateCountDto;
		for (IssueMilestone issueMilestone : issueMilestonePaginationResult) {
			Issue issue = issueMilestone.getIssue();
			if (issue.getMilestone() > 0) {
				Milestone milestone = milestoneDao.find_milestone_with_id(issue
						.getMilestone());
				if (milestone != null) {
					cateCountDto = milestone.getCate_count();
				} else {
					cateCountDto = new MilestoneCateCountDto();
				}
				cateCountDto.setOpen(cateCountDto.getOpen() + 1);
				cateCountDto.setClosed(cateCountDto.getClosed() - 1);
				milestone.setCate_count(cateCountDto);
				milestoneDao.update_milestone(milestone);
			}
		}
		sqlSession.flushStatements();
		sqlSession.commit();
		sqlSession.close();
	}
}
